function [sample_purchase sample_price_origin mom sample_country sample_net_sale  ann_sale ann_price ann_purchase ann_rating] = simulate_one(SIMINPUT, T_cal, delta, h_theta, h_eps, ave_theta, ...
    theta_firm, theta_grid_mat, pr_theta_firm,pr1_mat,pr2_mat, price_policy, cost, gamma,step)
% major assumptions:
% exit firms not observable; only observe existed firms 
% only observe price for existing firms

global num_firm T_sim S T n_theta  beta n_tran tau mktfe search rho_par country_num
replace = SIMINPUT.replace;
country = SIMINPUT.country;
exit = SIMINPUT.exit;
purchase = SIMINPUT.purchase;
ads = SIMINPUT.ads;
feedback = SIMINPUT.feedback;
sigma = 0.17* cost*exp(ave_theta);
rho = rho_par*sigma;
% initialize simulated records
    firm_exit = nan(num_firm,T_sim);
    firm_age = nan(num_firm,T_sim);
    firm_true = nan(num_firm,T_sim);
    firm_ad = nan(num_firm,T_sim);
    firm_state_ind = nan(num_firm,T_sim,country_num);
    firm_price = nan(num_firm,T_sim);
    firm_net_price = nan(num_firm,T_sim);
    firm_country = nan(num_firm,T_sim);
    firm_purchase = nan(num_firm,T_sim);
    firm_purchase_pr = nan(num_firm,country_num*T_sim);
    firm_imp = nan(num_firm,country_num*T_sim);
    firm_imp_ave = nan(num_firm,T_sim);
 
    % simulate first period
%    parfor j = 1:num_firm
  for j = 1:num_firm
        true_v = nan(1,T_sim);
        ad_v = nan(1,T_sim);
        purchase_v = zeros(1,T_sim);
        age_v = nan(1,T_sim);
        priceuse_v = nan(1,country_num);
        price_v = nan(1, T_sim);
        net_price_v = nan(1, T_sim);
        country_v = nan(1,T_sim);
        state_ind_v = nan(1,country_num*T_sim);
        purchase_pr_v = nan(1,country_num*T_sim);
        exit_v = nan(1,T_sim);
        imp_v = nan(1,country_num*T_sim);
        pr_purchase = nan(1, country_num);
        
 
        % initialize the firm
        exit_v(1) = 2;
        age_v(1) = 0;
        firm_ind = transform(pr_theta_firm', replace(j,1));
        true_v(1) = theta_firm(firm_ind);
        state_ind_v(1,1:country_num) = transform( reshape(pr1_mat( :,firm_ind ),n_tran,country_num),repmat(ads(j,1),1,country_num) );  %1 by countrynum*Tsim
        priceuse_v = price_policy( (0:country_num-1)*n_tran+state_ind_v(1,1:country_num),age_v(1)+1,firm_ind)'; %1 by countrynum
        imp_v(1,1:country_num) = exp(theta_grid_mat(state_ind_v(1,1:country_num),age_v(1)+1,firm_ind)); %1 by countrynum*Tsim
        pr_purchase = exp(1/sigma*(rho*imp_v(1,1:country_num)- priceuse_v))./mktfe'; %1 by countrynum*Tsim
        
        country_v(1) = country(j,1);
		pr_purchase(find(pr_purchase>1)) = 0.99;
        purchase_v(1) = transform([pr_purchase(country_v(1));1-pr_purchase(country_v(1))], purchase(j,1)); %1=purchase, 2=not purchase
        purchase_pr_v(1,1:country_num) = pr_purchase; %1 by countrynum*Tsim
        price_v(1) = priceuse_v(1,country_v(1)); %1 by Tsim, already select the country where the buyer is from
        net_price_v(1) = price_v(1) - tau(country_v(1));%1 by Tsim, already select the country where the buyer is from, exclude transportation cost
        for k=2:T_sim
            % firm exit?
            exit_v(k) = exit(j,k); % 1 = exit; 2= stay
            if exit_v(k) == 1
                age_v(k) = 0;
                firm_ind = transform(pr_theta_firm', replace(j,k));
                true_v(k) = theta_firm(firm_ind);
                    state_ind_v(1,((k-1)*country_num+1):k*country_num ) = transform( reshape(pr1_mat( :,firm_ind ),n_tran,country_num),repmat(ads(j,k),1,country_num) );%1 by countrynum*Tsim
            
            else
                true_v(1,k) = true_v(1,k-1);
                firm_ind = find(theta_firm == true_v(1,k));
                ad_v(1,k) = ad_v(1,k-1);
                if purchase_v(1,k-1) == 1
                    age_v(k) = age_v(k-1)+1;
                    if age_v(k)>T
                        age_v(k) = T;
                    end
                    sprintf('firm_ind: %2.0f, firm_age(j,k):%2.0f, firm_state_ind(j,k-1):%2.0f\n',firm_ind,age_v(k),state_ind_v(k-1));
                    state_ind_v(1,((k-1)*country_num+1):k*country_num ) = transform( reshape(pr2_mat(n_tran*(age_v(1,k)-1)+state_ind_v(1,k-1),:,firm_ind)',n_tran,country_num),repmat(feedback(j,k),1,country_num) )';
                else
                    age_v(1,k) = age_v(1,k-1);
                    state_ind_v(1,((k-1)*country_num+1):(k*country_num)) = state_ind_v(1,((k-2)*country_num+1):(k-1)*country_num); %1 by countrynum*Tsim
                end
            end
          
                 priceuse_v = price_policy(n_tran*((1:country_num)-1)+state_ind_v(1,(k-1)*country_num+(1:country_num)),age_v(k)+1,firm_ind)';
                 imp_v(1,(k-1)*country_num+1:k*country_num) = exp(theta_grid_mat(state_ind_v(1,(k-1)*country_num+1:k*country_num),age_v(k)+1,firm_ind));
                pr_purchase(1,:) = exp(1/sigma*(rho*imp_v(1,(k-1)*country_num+1:k*country_num)- priceuse_v))...
                    ./mktfe';
             
                
                country_v(1,k) = country(j,k);
           		pr_purchase(find(pr_purchase>1)) = 0.99;
                price_v(1,k) = priceuse_v(1,country_v(1,k)); %1 by Tsim, already select the right country
                net_price_v(1,k) = price_v(1,k)-tau(country_v(1,k)); % 1 by Tsim, already select the right country, exclude transportation
                purchase_pr_v(1,((k-1)*country_num+1):(k*country_num) ) = pr_purchase; %1 by countrynum*Tsim
        
        purchase_v(1,k) = transform([pr_purchase(country_v(k));1-pr_purchase(country_v(k))], purchase(j,k)); %1=purchase, 2=not purchase
           
        end
        firm_imp_ave(j,:) = mean(reshape(imp_v,country_num,T_sim),1)';
        firm_imp(j,:) = imp_v; %1 by countrynum*Tsim
        firm_true(j,:) = true_v;%1 by Tsim
        firm_ad(j,:) = ad_v;%1 by Tsim
        firm_purchase(j,:) = purchase_v; %1 by Tsim
        firm_age(j,:) = age_v; %1 by Tsim
        firm_price(j,:) = price_v; %1 by Tsim already select the country the buyer is from
        firm_net_price(j,:) = net_price_v; %1 by Tsim already select the country the buyer is from, exclude transportation cost
        firm_country(j,:) = country_v; %1 by Tsim
        firm_state_ind(j,:) = state_ind_v; %1 by countrynum*Tsim
        firm_exit(j,:) = exit_v; % 1 by Tsim
        firm_purchase_pr(j,:) = purchase_pr_v;%1 by countrynum*Tsim
    end
%     start_ind = 1;
%     end_ind = 50;
     start_ind = T_sim-T_cal+1;
     end_ind = T_sim;
%    firm_price(:,1:25)
    firm_purchase_pr(:,start_ind*country_num:end_ind*country_num);
    % compute moments for last T_cal periods
    sample_age = firm_age(:,start_ind:end_ind);
    sample_exit = firm_exit(:,start_ind:end_ind); %1=exit, 2= exist
    sample_exist_ind = zeros(size(sample_exit));
    sample_exist_ind(find(sample_exit==2)) = 1;
     sample_exist_final = sample_exist_ind;
    for ii = 1:size(sample_exist_ind,1)
        lastind = find(sample_exist_ind(ii,:)==0,1);
        if isempty(lastind)
            lastind = size(sample_exist_ind,2);
        end;
        sample_exist_final(ii,(lastind):size(sample_exist_ind,2)) = 0; %1 = stay; 0 = exit
    end;
      
   sample_imp_ave = firm_imp_ave(:,start_ind:end_ind).*sample_exist_final;
    sample_country = firm_country(:,start_ind:end_ind).*sample_exist_final;
     sample_price_origin = firm_price(:,start_ind:end_ind).*sample_exist_final;
    sample_price_net = firm_net_price(:,start_ind:end_ind).*sample_exist_final;
    sample_purchase = 2 - firm_purchase(:,start_ind:end_ind); %purchase is 1 and not purchase is 0
%    sample_country = firm_country(:,start_ind:end_ind);
    sample_sale = sample_purchase.*sample_price_origin;
    sample_net_sale = sample_purchase.*sample_price_net;
    sample_purchase(find(sample_exist_final==0)) = nan;
	sample_sale(find(sample_exist_final==0))=nan;
    sample_net_sale(find(sample_exist_final==0))=nan;
	lnsample_sale = log(sample_sale+1);
    lnsample_net_sale = log(sample_sale+1);
    
% calculate sales pct and growth
    
    sample_price(find(sample_exist_final==0))=nan;
    sample_imp_ave(find(sample_exist_final==0))=nan;
    
    [ann_sale ann_price ann_purchase ann_rating] = annual(num_firm,sample_net_sale,sample_price_net,sample_purchase,sample_imp_ave);
%    [month_sale month_price] = month(num_firm,sample_sale,sample_price_origin,sample_purchase);
    [mom1] = getmom(ann_sale,ann_price,ann_purchase);
%    [mom2] = getmom(month_sale,month_price);
    mom = [mom1];
    if step==2
        for ii = 1:country_num
           [ann_sale ann_price ann_purchase] = annual(num_firm, sample_net_sale.*(sample_country==ii), sample_price_net.*(sample_country==ii), sample_purchase.*(sample_country==ii),sample_imp_ave.*(sample_country==ii));
           tmpmom = getmom(ann_sale,ann_price,ann_purchase);
           mom = [mom;tmpmom];
        end
    end
end